package com.devteam.weatherforcecast;

import java.util.ArrayList;
import java.util.Date;
import com.devteam.weather.WeatherCurrentCondition;
import com.devteam.weather.WeatherForecastCondition;
import android.app.Activity;
import android.app.Dialog;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class WeatherForecastActivity extends Activity {
	private WeatherUtils wu;
	private String b;
	SharedPreferences prefs;
	private String preName = "Mypres";
	private static final String TEXT_VALUE_KEY = "text";
	private GoogleWeatherHandler a;
	static final private int SETTING = Menu.FIRST;
	static final private int UPDATE = Menu.FIRST + 1;
	static final private int UPDATE1 = 1;
	static final private int SETTING1 = 2;
	static final private int HELP1 = 3;
	static final private int ABOUT1 = 4;
	static String UPDATE_ACTION = "ABC";
	static final private int BACK = Menu.CATEGORY_SECONDARY;
	static final private int ABOUT = Menu.CATEGORY_SECONDARY + 1;
	static final private int HELP = Menu.CATEGORY_ALTERNATIVE;
	private TextView tvt_diadiem;
	private Date date = new Date();
	private Dialog dialog;
	/**
	 * Danh sách các dự báo của các ngày tiếp theo
	 */
	private ArrayList<WeatherForecastCondition> listWeatherForecast;
	/**
	 * Dự báo của ngày hiện tại
	 */
	private WeatherCurrentCondition weatherCurrent;
	private String linkData;
	/*
	 * Tham số ngày hiện tại
	 */
	private TextView tvt_ngaythang_hientai, tvt_nhietdo_hientai,
			tvt_troi_hientai, tvt_doam_hientai, tvt_gio_hientai;
	private ImageView im_trangthai_hientai;
	/*
	 * Tham số các ngày tiếp theo
	 */
	private TextView tvt_ngaytiep_1, tvt_ngaytiep_2, tvt_ngaytiep_3,
			tvt_troi_ngaytiep_1, tvt_troi_ngaytiep_2, tvt_troi_ngaytiep_3,
			tvt_nhietdo_Min_ngaytiep_1, tvt_nhietdo_Min_ngaytiep_2,
			tvt_nhietdo_Min_ngaytiep_3, tvt_nhietdo_Max_ngaytiep_1,
			tvt_nhietdo_Max_ngaytiep_2, tvt_nhietdo_Max_ngaytiep_3;
	private ImageView im_troi_ngaytiep_1, im_troi_ngaytiep_2,
			im_troi_ngaytiep_3;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.menu);
		tvt_diadiem = (TextView) findViewById(R.id.tv_diadiem);
		tvt_ngaythang_hientai = (TextView) findViewById(R.id.tv_ngaythang_hientai);
		tvt_nhietdo_hientai = (TextView) findViewById(R.id.tv_nhietdo_hientai);
		tvt_troi_hientai = (TextView) findViewById(R.id.tv_troi_hientai);
		tvt_doam_hientai = (TextView) findViewById(R.id.tv_doam_hientai);
		tvt_gio_hientai = (TextView) findViewById(R.id.tv_gio_hientai);
		im_trangthai_hientai = (ImageView) findViewById(R.id.im_trangthai_hientai);
		tvt_ngaytiep_1 = (TextView) findViewById(R.id.tv_ngaytiep_1);
		tvt_ngaytiep_2 = (TextView) findViewById(R.id.tv_ngaytiep_2);
		tvt_ngaytiep_3 = (TextView) findViewById(R.id.tv_ngaytiep_3);
		tvt_troi_ngaytiep_1 = (TextView) findViewById(R.id.tv_troi_ngaytiep_1);
		tvt_troi_ngaytiep_2 = (TextView) findViewById(R.id.tv_troi_ngaytiep_2);
		tvt_troi_ngaytiep_3 = (TextView) findViewById(R.id.tv_troi_ngaytiep_3);
		tvt_nhietdo_Min_ngaytiep_1 = (TextView) findViewById(R.id.tv_nhietdo_Min_ngaytiep_1);
		tvt_nhietdo_Min_ngaytiep_2 = (TextView) findViewById(R.id.tv_nhietdo_Min_ngaytiep_2);
		tvt_nhietdo_Min_ngaytiep_3 = (TextView) findViewById(R.id.tv_nhietdo_Min_ngaytiep_3);
		tvt_nhietdo_Max_ngaytiep_1 = (TextView) findViewById(R.id.tv_nhietdo_Max_ngaytiep_1);
		tvt_nhietdo_Max_ngaytiep_2 = (TextView) findViewById(R.id.tv_nhietdo_Max_ngaytiep_2);
		tvt_nhietdo_Max_ngaytiep_3 = (TextView) findViewById(R.id.tv_nhietdo_Max_ngaytiep_3);
		im_troi_ngaytiep_1 = (ImageView) findViewById(R.id.im_troi_ngaytiep_1);
		im_troi_ngaytiep_2 = (ImageView) findViewById(R.id.im_troi_ngaytiep_2);
		im_troi_ngaytiep_3 = (ImageView) findViewById(R.id.im_troi_ngaytiep_3);
		wu = new WeatherUtils();
		listWeatherForecast = new ArrayList<WeatherForecastCondition>();
		weatherCurrent = new WeatherCurrentCondition();
		linkData = "http://www.google.com/ig/api?weather=HaNoi";
		GoogleWeatherHandler a = new GoogleWeatherHandler(listWeatherForecast,
				weatherCurrent, linkData);
		a.parseData();
		Show_Weather_Current(a, "Ha Noi");
		Show_Weather_Continue(a);
	}

	/*
	 * Cài đặt thời tiết các ngày tiếp theo
	 */
	public void Show_Weather_Continue(GoogleWeatherHandler a) {

		tvt_ngaytiep_1.setText(wu.convertToTV(a.listWeatherForecast.get(1)
				.getNgayTrongTuan()));
		tvt_ngaytiep_2.setText(wu.convertToTV(a.listWeatherForecast.get(2)
				.getNgayTrongTuan()));
		tvt_ngaytiep_3.setText(wu.convertToTV(a.listWeatherForecast.get(3)
				.getNgayTrongTuan()));
		tvt_nhietdo_Min_ngaytiep_1.setText(String.valueOf(wu
				.convertCtoF(a.listWeatherForecast.get(1).getNhietDoMin()))
				+ "°C");
		tvt_nhietdo_Min_ngaytiep_2.setText(String.valueOf(wu
				.convertCtoF(a.listWeatherForecast.get(2).getNhietDoMin()))
				+ "°C");
		tvt_nhietdo_Min_ngaytiep_3.setText(String.valueOf(wu
				.convertCtoF(a.listWeatherForecast.get(3).getNhietDoMin()))
				+ "°C");
		tvt_nhietdo_Max_ngaytiep_1.setText(String.valueOf(wu
				.convertCtoF(a.listWeatherForecast.get(1).getNhietDoMax()))
				+ "°C");
		tvt_nhietdo_Max_ngaytiep_2.setText(String.valueOf(wu
				.convertCtoF(a.listWeatherForecast.get(2).getNhietDoMax()))
				+ "°C");
		tvt_nhietdo_Max_ngaytiep_3.setText(String.valueOf(wu
				.convertCtoF(a.listWeatherForecast.get(3).getNhietDoMax()))
				+ "°C");

		if (a.listWeatherForecast.get(3).getTinhTrang().equals("Cloudy")) {
			tvt_troi_ngaytiep_3.setText("Có mây");
			im_troi_ngaytiep_3.setImageResource(R.drawable.symbol_1_3d);

		} else if (a.listWeatherForecast.get(3).getTinhTrang()
				.equals("Thunderstorm")) {
			tvt_troi_ngaytiep_3.setText("Có dông");
			im_troi_ngaytiep_3.setImageResource(R.drawable.symbol_1_5d);

		} else if (a.listWeatherForecast.get(3).getTinhTrang().equals("Sunny")) {
			tvt_troi_ngaytiep_3.setText("Nắng vừa");
			im_troi_ngaytiep_3.setImageResource(R.drawable.symbol_1_1d);

		} else if (a.listWeatherForecast.get(3).getTinhTrang()
				.equals("Partly Sunny")) {
			tvt_troi_ngaytiep_3.setText("Nắng nhẹ");
			im_troi_ngaytiep_3.setImageResource(R.drawable.symbol_1_1d);

		} else if (a.listWeatherForecast.get(3).getTinhTrang()
				.equals("Mostly Sunny")) {
			tvt_troi_ngaytiep_3.setText("Nắng gắt");
			im_troi_ngaytiep_3.setImageResource(R.drawable.symbol_1_1d);

		} else if (a.listWeatherForecast.get(3).getTinhTrang()
				.equals("Chance of Rain")) {
			tvt_troi_ngaytiep_3.setText("Có thể mưa");
			im_troi_ngaytiep_3.setImageResource(R.drawable.symbol_1_18d);

		} else if (a.listWeatherForecast.get(3).getTinhTrang().equals("Clear")) {
			tvt_troi_ngaytiep_3.setText("Trong sáng");
			im_troi_ngaytiep_3.setImageResource(R.drawable.symbol_1_2d);

		} else if (a.listWeatherForecast.get(3).getTinhTrang()
				.equals("Light rain")) {
			tvt_troi_ngaytiep_3.setText("Mưa nhỏ");
			im_troi_ngaytiep_3.setImageResource(R.drawable.symbol_1_9d);

		} else if (a.listWeatherForecast.get(3).getTinhTrang().equals("Rain")) {
			tvt_troi_ngaytiep_3.setText("Mưa");
			im_troi_ngaytiep_3.setImageResource(R.drawable.symbol_1_10d);

		} else if (a.listWeatherForecast.get(3).getTinhTrang()
				.equals("Chance of Storm")) {
			tvt_troi_ngaytiep_3.setText("Có thể bão");
			im_troi_ngaytiep_3.setImageResource(R.drawable.symbol_1_11d);

		} else if (a.listWeatherForecast.get(3).getTinhTrang()
				.equals("Mostly Cloudy")) {
			tvt_troi_ngaytiep_3.setText("Nhiều mây");
			im_troi_ngaytiep_3.setImageResource(R.drawable.symbol_1_4d);
		}

		if (a.listWeatherForecast.get(1).getTinhTrang().equals("Cloudy")) {
			tvt_troi_ngaytiep_1.setText("Có mây");
			im_troi_ngaytiep_1.setImageResource(R.drawable.symbol_1_3d);

		} else if (a.listWeatherForecast.get(1).getTinhTrang()
				.equals("Light rain")) {
			tvt_troi_ngaytiep_1.setText("Mưa nhỏ");
			im_troi_ngaytiep_1.setImageResource(R.drawable.symbol_1_9d);

		} else if (a.listWeatherForecast.get(1).getTinhTrang().equals("Rain")) {
			tvt_troi_ngaytiep_1.setText("Mưa");
			im_troi_ngaytiep_1.setImageResource(R.drawable.symbol_1_10d);

		} else if (a.listWeatherForecast.get(1).getTinhTrang().equals("Clear")) {
			tvt_troi_ngaytiep_1.setText("Trong xanh");
			im_troi_ngaytiep_1.setImageResource(R.drawable.symbol_1_2d);

		} else if (a.listWeatherForecast.get(1).getTinhTrang()
				.equals("Chance of Rain")) {
			tvt_troi_ngaytiep_1.setText("Có thể mưa");
			im_troi_ngaytiep_1.setImageResource(R.drawable.symbol_1_18d);

		} else if (a.listWeatherForecast.get(1).getTinhTrang().equals("Sunny")) {
			tvt_troi_ngaytiep_1.setText("Nắng vừa");
			im_troi_ngaytiep_1.setImageResource(R.drawable.symbol_1_1d);

		} else if (a.listWeatherForecast.get(1).getTinhTrang()
				.equals("Partny Sunny")) {
			tvt_troi_ngaytiep_1.setText("Nắng nhẹ");
			im_troi_ngaytiep_1.setImageResource(R.drawable.symbol_1_1d);

		} else if (a.listWeatherForecast.get(1).getTinhTrang()
				.equals("Mostly Sunny")) {
			tvt_troi_ngaytiep_1.setText("Nắng gắt");
			im_troi_ngaytiep_1.setImageResource(R.drawable.symbol_1_1d);

		} else if (a.listWeatherForecast.get(1).getTinhTrang()
				.equals("Thunderstorm")) {
			tvt_troi_ngaytiep_1.setText("Có dông");
			im_troi_ngaytiep_1.setImageResource(R.drawable.symbol_1_5d);

		} else if (a.listWeatherForecast.get(1).getTinhTrang()
				.equals("Chance of Storm")) {
			tvt_troi_ngaytiep_1.setText("Có thể bão");
			im_troi_ngaytiep_1.setImageResource(R.drawable.symbol_1_11d);

		} else if (a.listWeatherForecast.get(1).getTinhTrang()
				.equals("Mostly Cloudy")) {
			tvt_troi_ngaytiep_1.setText("Nhiều mây");
		}

		if (a.listWeatherForecast.get(2).getTinhTrang().equals("Cloudy")) {
			tvt_troi_ngaytiep_2.setText("Có mây");
			im_troi_ngaytiep_2.setImageResource(R.drawable.symbol_1_3d);

		} else if (a.listWeatherForecast.get(2).getTinhTrang()
				.equals("Light rain")) {
			tvt_troi_ngaytiep_2.setText("Mưa nhỏ");
			im_troi_ngaytiep_2.setImageResource(R.drawable.symbol_1_9d);

		} else if (a.listWeatherForecast.get(2).getTinhTrang().equals("Rain")) {
			tvt_troi_ngaytiep_2.setText("Mưa");
			im_troi_ngaytiep_2.setImageResource(R.drawable.symbol_1_10d);

		} else if (a.listWeatherForecast.get(2).getTinhTrang()
				.equals("Chance of Rain")) {
			tvt_troi_ngaytiep_2.setText("Có thể mưa");
			im_troi_ngaytiep_2.setImageResource(R.drawable.symbol_1_18d);

		} else if (a.listWeatherForecast.get(2).getTinhTrang().equals("Clear")) {
			tvt_troi_ngaytiep_2.setText("Trong xanh");
			im_troi_ngaytiep_2.setImageResource(R.drawable.symbol_1_2d);

		} else if (a.listWeatherForecast.get(2).getTinhTrang().equals("Sunny")) {
			tvt_troi_ngaytiep_2.setText("Nắng vừa");
			im_troi_ngaytiep_2.setImageResource(R.drawable.symbol_1_1d);

		} else if (a.listWeatherForecast.get(2).getTinhTrang()
				.equals("Partly Sunny")) {
			tvt_troi_ngaytiep_2.setText("Nắng nhẹ");
			im_troi_ngaytiep_2.setImageResource(R.drawable.symbol_1_1d);

		} else if (a.listWeatherForecast.get(2).getTinhTrang()
				.equals("Mostly Sunny")) {
			tvt_troi_ngaytiep_2.setText("Nắng gắt");
			im_troi_ngaytiep_2.setImageResource(R.drawable.symbol_1_1d);

		} else if (a.listWeatherForecast.get(2).getTinhTrang()
				.equals("Thunderstorm")) {
			tvt_troi_ngaytiep_2.setText("Có dông");
			im_troi_ngaytiep_2.setImageResource(R.drawable.symbol_1_5d);

		} else if (a.listWeatherForecast.get(2).getTinhTrang()
				.equals("Chance of Storm")) {
			tvt_troi_ngaytiep_2.setText("Có thể bão");
			im_troi_ngaytiep_2.setImageResource(R.drawable.symbol_1_11d);

		} else if (a.listWeatherForecast.get(2).getTinhTrang()
				.equals("Mostly Cloudy")) {
			tvt_troi_ngaytiep_2.setText("Nhiều mây");
			im_troi_ngaytiep_2.setImageResource(R.drawable.symbol_1_4d);
		}
	}

	/*
	 * Cài đặt thời tiết hiện thời
	 */
	public void Show_Weather_Current(GoogleWeatherHandler a, String diadiem) {
		tvt_diadiem.setText(diadiem);
		tvt_ngaythang_hientai.setText(wu.convertToTV(a.listWeatherForecast.get(
				0).getNgayTrongTuan())
				+ "," + a.weatherCurrent2.getNgayCuaTuan());

		if (a.weatherCurrent2.getTinhTrang().equals("Cloudy")) {
			tvt_troi_hientai.setText("Có mây");
			if (date.getHours() <= 18) {
				im_trangthai_hientai.setImageResource(R.drawable.symbol_1_3d);
			} else
				im_trangthai_hientai.setImageResource(R.drawable.symbol_1_3n);

		} else if (a.weatherCurrent2.getTinhTrang().equals("Rain")) {
			tvt_troi_hientai.setText("Có mưa");
			if (date.getHours() <= 18)
				im_trangthai_hientai.setImageResource(R.drawable.symbol_1_9d);
			else
				im_trangthai_hientai.setImageResource(R.drawable.symbol_1_9n);

		} else if (a.weatherCurrent2.getTinhTrang().equals("Light rain")) {
			tvt_troi_hientai.setText("Mưa nhỏ");
			if (date.getHours() <= 18)
				im_trangthai_hientai.setImageResource(R.drawable.symbol_1_9d);
			else
				im_trangthai_hientai.setImageResource(R.drawable.symbol_1_9n);

		} else if (a.weatherCurrent2.getTinhTrang().equals("Overcast")) {
			tvt_troi_hientai.setText("U ám");
			if (date.getHours() <= 18)
				im_trangthai_hientai.setImageResource(R.drawable.symbol_1_4d);
			else
				im_trangthai_hientai.setImageResource(R.drawable.symbol_1_4n);

		} else if (a.weatherCurrent2.getTinhTrang().equals("Thunderstorm")) {
			tvt_troi_hientai.setText("Có dông");
			if (date.getHours() <= 18)
				im_trangthai_hientai.setImageResource(R.drawable.symbol_1_5d);
			else
				im_trangthai_hientai.setImageResource(R.drawable.symbol_1_5n);

		} else if (a.weatherCurrent2.getTinhTrang().equals("Chance of Storm")) {
			tvt_troi_hientai.setText("Có thể bão");
			if (date.getHours() <= 18)
				im_trangthai_hientai.setImageResource(R.drawable.symbol_1_11d);
			else
				im_trangthai_hientai.setImageResource(R.drawable.symbol_1_11n);

		} else if (a.weatherCurrent2.getTinhTrang().equals("Mostly Cloudy")) {
			tvt_troi_hientai.setText("Nhiều mây");
			if (date.getHours() <= 18) {
				im_trangthai_hientai.setImageResource(R.drawable.symbol_1_15d);
			} else
				im_trangthai_hientai.setImageResource(R.drawable.symbol_1_15n);

		} else if (a.weatherCurrent2.getTinhTrang().equals("Clear")) {
			tvt_troi_hientai.setText("Trong sáng");
			if (date.getHours() <= 18) {
				im_trangthai_hientai.setImageResource(R.drawable.symbol_1_2d);
			} else
				im_trangthai_hientai.setImageResource(R.drawable.symbol_1_2n);

		} else if (a.weatherCurrent2.getTinhTrang().equals("Mostly Sunny")) {
			tvt_troi_hientai.setText("Nắng gắt");
			if (date.getHours() <= 18) {
				im_trangthai_hientai.setImageResource(R.drawable.symbol_1_1d);
			} else
				im_trangthai_hientai.setImageResource(R.drawable.symbol_1_1n);

			// Partly Sunny
		} else if (a.weatherCurrent2.getTinhTrang().equals("Partly Sunny")) {
			tvt_troi_hientai.setText("Nắng nhẹ");
			if (date.getHours() <= 18) {
				im_trangthai_hientai.setImageResource(R.drawable.symbol_1_1d);
			} else
				im_trangthai_hientai.setImageResource(R.drawable.symbol_1_1n);

		} else if (a.weatherCurrent2.getTinhTrang().equals("Sunny")) {
			tvt_troi_hientai.setText("Nắng vừa");
			if (date.getHours() <= 18) {
				im_trangthai_hientai.setImageResource(R.drawable.symbol_1_1d);
			} else
				im_trangthai_hientai.setImageResource(R.drawable.symbol_1_1n);

		} else if (a.weatherCurrent2.getTinhTrang().equals("Chance of Rain")) {
			tvt_troi_hientai.setText("Có thể mưa");
			if (date.getHours() <= 18) {
				im_trangthai_hientai.setImageResource(R.drawable.symbol_1_18d);
			} else
				im_trangthai_hientai.setImageResource(R.drawable.symbol_1_18n);
		}

		tvt_nhietdo_hientai.setText(a.weatherCurrent2.getNhietDoC().toString()
				+ "°C" + "/" + a.weatherCurrent2.getNhietDoF().toString()
				+ "°F");
		try {
			tvt_doam_hientai.setText(a.weatherCurrent2.getDoAm());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			tvt_gio_hientai.setText(a.weatherCurrent2.getTinhTrangGio());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/*
	 * Tạo Menu điều khiển
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		super.onCreateOptionsMenu(menu);
		MenuItem itemset = menu.add(0, SETTING, Menu.NONE, "Setting");
		MenuItem itemupdate = menu.add(0, UPDATE, Menu.NONE, "Update");
		MenuItem itemupback = menu.add(0, BACK, Menu.NONE, "Back");
		MenuItem itemupedit = menu.add(0, ABOUT, Menu.NONE, "About");
		MenuItem itemuphelp = menu.add(0, HELP, Menu.NONE, "Help");
		itemset.setIcon(R.drawable.weather_setting);
		itemupdate.setIcon(R.drawable.weather_update);
		itemupback.setIcon(R.drawable.weather_back);
		itemupedit.setIcon(R.drawable.weather_edit);
		itemuphelp.setIcon(R.drawable.weather_help);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		super.onOptionsItemSelected(item);
		switch (item.getItemId()) {
		case UPDATE:
			showDialog(UPDATE1);
			break;
		case SETTING:
			showDialog(SETTING1);
			break;
		case BACK:
			System.exit(0);
			break;
		case ABOUT:
			showDialog(ABOUT1);
			break;
		case HELP:
			showDialog(HELP1);
			break;
		}
		return true;
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		// TODO Auto-generated method stub
		switch (id) {
		case ABOUT1:
			dialog = new Dialog(WeatherForecastActivity.this, R.style.Dialog1);
			dialog.setContentView(R.layout.dialog_about);
			Button bt_cancel1 = (Button) dialog.findViewById(R.id.bt_cancel);
			bt_cancel1.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
				dialog.dismiss();	
				}
			});
			break;
		case HELP1:
			dialog = new Dialog(WeatherForecastActivity.this, R.style.Dialog1);
			dialog.setContentView(R.layout.dialog_help);
			TextView tv_help=(TextView) dialog.findViewById(R.id.tv_help);
			StringBuilder sb=new StringBuilder();
			sb.append("1: Người sử dụng có 2 chức năng :").append("\n"); 
					sb.append("   - Chọn vùng muốn xem thời tiết chi tiết (Chọn Menu-Search...)").append("\n");
					sb.append("   - Chọn vùng muốn cài đặt trên Widget (Chọn Menu-Setting...)").append("\n");
			sb.append("2. Sau khi đã Search hoặc Setting! Người dùng thực hiện nhấn nút Back cứng để quay lại màn hình xem!");
			tv_help.setText(sb);
			Button bt_cancel2 = (Button) dialog.findViewById(R.id.bt_cancel);
			bt_cancel2.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
				dialog.dismiss();	
				}
			});
			break;
		case UPDATE1:
			dialog = new Dialog(WeatherForecastActivity.this, R.style.Dialog1);
			dialog.setContentView(R.layout.dialog_search);
			Button bt_search = (Button) dialog.findViewById(R.id.bt_search);
			bt_search.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					wu = new WeatherUtils();
					EditText ed_diadiem = (EditText) dialog
							.findViewById(R.id.ed_diadiem);
					b = ed_diadiem.getText().toString();
					while (b.indexOf("  ") > 0) {
						b = b.replace("  ", " ");
					}
					b = b.toLowerCase();

					String[] temp = b.split(" ");
					b = "";
					for (int i = 0; i < temp.length; i++) {
						b = b + temp[i].substring(0, 1).toUpperCase()
								+ temp[i].substring(1, temp[i].length()) + " ";
					}
					String s1 = b.replaceAll(" ", "");
					linkData = "http://www.google.com/ig/api?weather=" + s1;
					listWeatherForecast = new ArrayList<WeatherForecastCondition>();
					weatherCurrent = new WeatherCurrentCondition();
					a = new GoogleWeatherHandler(listWeatherForecast,
							weatherCurrent, linkData);
					a.parseData();
					if (listWeatherForecast.size() > 0) {
						ed_diadiem.setText("");
						dialog.dismiss();
						Show_Weather_Current(a, b);
						Show_Weather_Continue(a);
					} else {
						String s2 = b.replaceAll(" ", "%20");
						linkData = "http://www.google.com/ig/api?weather=" + s2;
						listWeatherForecast = new ArrayList<WeatherForecastCondition>();
						weatherCurrent = new WeatherCurrentCondition();
						a = new GoogleWeatherHandler(listWeatherForecast,
								weatherCurrent, linkData);
						a.parseData();
						if (listWeatherForecast.size() > 0) {
							ed_diadiem.setText("");
							dialog.dismiss();
							Show_Weather_Current(a, b);
							Show_Weather_Continue(a);
						} else {
							String s3 = b.replaceAll(" ", "%20");
							String s4 = s3 + ",viet%20nam";
							linkData = "http://www.google.com/ig/api?weather="
									+ s4;
							listWeatherForecast = new ArrayList<WeatherForecastCondition>();
							weatherCurrent = new WeatherCurrentCondition();
							a = new GoogleWeatherHandler(listWeatherForecast,
									weatherCurrent, linkData);
							a.parseData();
							if (listWeatherForecast.size() > 0) {
								ed_diadiem.setText("");
								dialog.dismiss();
								Show_Weather_Current(a, b);
								Show_Weather_Continue(a);
							} else {
								ed_diadiem.setText("");
								dialog.dismiss();
								Toast.makeText(getBaseContext(),
										"Không tìm thấy dữ liệu yêu cầu !",
										Toast.LENGTH_LONG).show();
							}
						}
					}
				}
			});
			break;
		case SETTING1:
			dialog = new Dialog(WeatherForecastActivity.this, R.style.Dialog1);
			dialog.setContentView(R.layout.dialog_setting);
			Button bt_setting = (Button) dialog.findViewById(R.id.bt_search);
			bt_setting.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					wu = new WeatherUtils();
					EditText ed_diadiem = (EditText) dialog
							.findViewById(R.id.ed_diadiem);
					b = ed_diadiem.getText().toString();
					while (b.indexOf("  ") > 0) {
						b = b.replace("  ", " ");
					}
					b = b.toLowerCase();

					String[] temp = b.split(" ");
					b = "";
					for (int i = 0; i < temp.length; i++) {
						b = b + temp[i].substring(0, 1).toUpperCase()
								+ temp[i].substring(1, temp[i].length()) + " ";
					}
					SharedPreferences prefs = getSharedPreferences(preName,
							MODE_PRIVATE);
					SharedPreferences.Editor editor = prefs.edit();
					editor.putString(TEXT_VALUE_KEY, b);
					editor.commit();
					String s1 = b.replaceAll(" ", "");
					linkData = "http://www.google.com/ig/api?weather=" + s1;
					listWeatherForecast = new ArrayList<WeatherForecastCondition>();
					weatherCurrent = new WeatherCurrentCondition();
					a = new GoogleWeatherHandler(listWeatherForecast,
							weatherCurrent, linkData);
					a.parseData();
					if (listWeatherForecast.size() > 0) {
						ed_diadiem.setText("");
						dialog.dismiss();
					} else {
						String s2 = b.replaceAll(" ", "%20");
						linkData = "http://www.google.com/ig/api?weather=" + s2;
						listWeatherForecast = new ArrayList<WeatherForecastCondition>();
						weatherCurrent = new WeatherCurrentCondition();
						a = new GoogleWeatherHandler(listWeatherForecast,
								weatherCurrent, linkData);
						a.parseData();
						if (listWeatherForecast.size() > 0) {
							ed_diadiem.setText("");
							dialog.dismiss();
							prefs = getSharedPreferences(preName, MODE_PRIVATE);
							SharedPreferences.Editor editor2 = prefs.edit();
							editor2.putString(TEXT_VALUE_KEY, s2);
							editor2.commit();
						} else {
							String s3 = b.replaceAll(" ", "%20");
							String s4 = s3 + ",viet%20nam";
							linkData = "http://www.google.com/ig/api?weather="
									+ s4;
							listWeatherForecast = new ArrayList<WeatherForecastCondition>();
							weatherCurrent = new WeatherCurrentCondition();
							a = new GoogleWeatherHandler(listWeatherForecast,
									weatherCurrent, linkData);
							a.parseData();
							if (listWeatherForecast.size() > 0) {
								ed_diadiem.setText("");
								prefs = getSharedPreferences(preName,
										MODE_PRIVATE);
								SharedPreferences.Editor editor3 = prefs.edit();
								editor3.putString(TEXT_VALUE_KEY, s4);
								editor3.commit();
								dialog.dismiss();
							} else {
								ed_diadiem.setText("");
								dialog.dismiss();
								Toast.makeText(getBaseContext(),
										"Không tìm thấy dữ liệu yêu cầu !",
										Toast.LENGTH_LONG).show();
							}
						}
					}
				}
			});
			break;
		}
		return dialog;

	}

	private void updateWidget() {
		AppWidgetManager appWidgetManager = AppWidgetManager
				.getInstance(getApplicationContext());
		ComponentName componentName = new ComponentName(
				getApplicationContext(), MainWidget.class);
		int[] ids = appWidgetManager.getAppWidgetIds(componentName);
		Intent i = new Intent(this, MainWidget.class);
		i.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
		i.setAction(MainWidget.UPDATE_ACTION);
		sendBroadcast(i);
		finish();
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		updateWidget();
		System.exit(0);
	}

	public static boolean checkNetworkAvailable(Context c) {
		// get connection manager from system service
		ConnectivityManager manager = (ConnectivityManager) c
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		// get network info status
		NetworkInfo netInfo = manager.getActiveNetworkInfo();
		// check availability status
		if (netInfo != null && netInfo.isAvailable()
				&& netInfo.isConnectedOrConnecting()) {
			return true;
		}
		return false;
	}

}
